//
//  YMFeedbackViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMFeedbackViewController.h"
#import "IQKeyboardManager.h"
#import "YMFeedbackInputTextCell.h"
#import "YMFeedbackAddPhotoCell.h"
#import "YMMineService.h"
#import "YMPhotoChooseManager.h"


@interface YMFeedbackViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) UIButton *btnFinish;

@property (nonatomic, strong) YMPhotoChooseManager *photoManager;

@property (nonatomic, copy) NSString *strContent;
@property (nonatomic, strong) NSMutableArray *arrPhoto;


@end

@implementation YMFeedbackViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    self.view.backgroundColor = STheme.D10;
    [self showNavTitle:KLanguage(@"feedback_txt")];
    
    [self.view addSubview:self.tvMain];
    [self.view addSubview:self.btnFinish];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.equalTo(self.btnFinish.mas_top).offset([SFitUtil fit:-10]);
    }];
    [self.btnFinish mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:310 height:45]);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT);
    }];
}

//提交
- (void)finishClickAction{
    if (self.strContent == nil || self.strContent.length < 0){
        [SHUD showInfo:KLanguage(@"feedback_advice_txt")];
        return;
    }
    WEAK_SELF;
    [YMMineService requestFeedbackDataWithContent:self.strContent imgUrls:[self.arrPhoto copy] success:^{
        [SHUD showInfo:KLanguage(@"submit_feedback_success_txt")];
        [SJumpUtil popVC:weakSelf animated:YES];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAK_SELF;
    if (indexPath.section == 0){
        YMFeedbackInputTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMFeedbackInputTextCell" forIndexPath:indexPath];
        cell.changeTextBlock = ^(NSString * _Nonnull text) {
            weakSelf.strContent = text;
        };
        return cell;
    }else {
        YMFeedbackAddPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMFeedbackAddPhotoCell" forIndexPath:indexPath];
        cell.arrPhoto = [self.arrPhoto copy];
        cell.deletePhotBlock = ^(NSInteger index) {
            if (index < weakSelf.arrPhoto.count){
                [weakSelf.arrPhoto removeObjectAtIndex:index];
                [weakSelf.tvMain reloadData];
            }
        };
        cell.addPhotBlock = ^{
            [weakSelf.photoManager choosePhotoAction:KLanguage(@"upload_image_txt") canVideo:NO vc:self];
        };
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)
    {
        return [YMFeedbackInputTextCell getCellHeight];
    }else {
        return [YMFeedbackAddPhotoCell getCellHeight:self.arrPhoto.count];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return [SFitUtil fit:15];
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        [_tvMain registerClass:[YMFeedbackInputTextCell class] forCellReuseIdentifier:@"YMFeedbackInputTextCell"];
        [_tvMain registerClass:[YMFeedbackAddPhotoCell class] forCellReuseIdentifier:@"YMFeedbackAddPhotoCell"];
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _tvMain;
}
- (UIButton *)btnFinish{
    if (_btnFinish == nil) {
        _btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnFinish setTitle:KLanguage(@"submit_txt") forState:UIControlStateNormal];
        [_btnFinish setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnFinish.titleLabel.font = [SFitUtil fitBoldFont:17];
        [_btnFinish addTarget:self action:@selector(finishClickAction) forControlEvents:UIControlEventTouchUpInside];
        _btnFinish.backgroundColor = STheme.D12;
        _btnFinish.clipsToBounds = YES;
        _btnFinish.layer.cornerRadius = [SFitUtil fit:10];
    }
    return _btnFinish;
}
- (YMPhotoChooseManager *)photoManager{
    if (_photoManager == nil){
        _photoManager = [[YMPhotoChooseManager alloc] init];
        WEAK_SELF;
        _photoManager.uploadSucessBlock = ^(NSString * _Nonnull url) {
            [weakSelf.arrPhoto addObject:[SDataUtil stringOf:url valid:@""]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                [weakSelf.tvMain reloadData];
            });
        };
    }
    return _photoManager;
}
- (NSMutableArray *)arrPhoto{
    if (_arrPhoto == nil){
        _arrPhoto = [[NSMutableArray alloc] init];
    }
    return _arrPhoto;
}


@end
